<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Hairstyleoptions extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('hairstyleoption');
	}
	public function ajax_hairstyleoption_get(){
		$hairstyleoptionEntity =  new CI_Hair_style_option_entity();
		$hairstyleoption = $hairstyleoptionEntity->getHairStyleByCode($_REQUEST['code']);
		echo json_encode($hairstyleoption);
	}
	public function ajax_hairstyleoption_list(){
		$hairstyleoptionEntity =  new CI_Hair_style_option_entity();
		$escortSettings = new CI_Escort_setting_entity();
		$defaultLanguageId = $escortSettings->getDefaultLanguageId();
		$hairstyleoptionList = $hairstyleoptionEntity->getHairStyleListByLanguage($defaultLanguageId);
		$data['hairstyleoptionList'] = $hairstyleoptionList;
		
		$languageEntity = new CI_Language_entity();
		$languageList = $languageEntity->getLanguageList();
		$data['languageList'] = $languageList;
		
		$this->load->view('admin/hairstyleoptions/index', $data);
	}
	public function ajax_hairstyleoption_save(){
		$hairstyleoptionNameList = $_REQUEST['hairstyleoption'];
		$hairstyleoptionNameDefault = '';
		foreach ($hairstyleoptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$hairstyleoptionNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($hairstyleoptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_hairstyleoption_require');
		}else{
			$code = 0;
			foreach ($hairstyleoptionNameList as $key => $value) {
				$hairstyleoptionEntity =  new CI_Hair_style_option_entity();
				if($value != null && $value != ''){
					$hairstyleoptionEntity->setHairColor($value);
				}else{
					$hairstyleoptionEntity->setHairColor($hairstyleoptionNameDefault);
				}
				$hairstyleoptionEntity->setLanguageId($key);
				$hairstyleoptionEntity->setCode($code);
				$id = $hairstyleoptionEntity->save();
				if($code == 0){
					$code = $id;
					$hairstyleoptionEntity->setId($id);
					$hairstyleoptionEntity->setCode($code);
					$hairstyleoptionEntity->update();
				}
			}
			$result['result'] = true;
			$result['message'] =lang('escort_hairstyleoption_save_success');
			$result['id'] =$id;
		}
		echo json_encode($result);
	}
	public function ajax_hairstyleoption_update(){
		$hairstyleoptionNameList = $_REQUEST['hairstyleoption'];
		$hairstyleoptionIdList = $_REQUEST['hairstyleoption_id'];
		$hairstyleoptionCode = $_REQUEST['hairstyleoption_code'];
		$hairstyleoptionNameDefault = '';
		foreach ($hairstyleoptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$hairstyleoptionNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($hairstyleoptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_hairstyleoption_require');
		}else{
			foreach ($hairstyleoptionNameList as $key => $value) {
				$hairstyleoptionEntity =  new CI_Hair_style_option_entity();
				if($value != null && $value != ''){
					$hairstyleoptionEntity->setHairColor($value);
				}else{
					$hairstyleoptionEntity->setHairColor($hairstyleoptionNameDefault);
				}
				$hairstyleoptionEntity->setLanguageId($key);
				$hairstyleoptionEntity->setCode($hairstyleoptionCode);
				$hairstyleoptionEntity->setId($hairstyleoptionIdList[$key]);
				$hairstyleoptionEntity->update();
			}
			$result['result'] = true;
			$result['message'] =lang('escort_hairstyleoption_save_success');
		}
		
		echo json_encode($result);
	}
	public function ajax_hairstyleoption_delete(){
		$result = array();
		$hairstyleoptionEntity =  new CI_Hair_style_option_entity();
		$result['result'] = $hairstyleoptionEntity->deleteByCode($_REQUEST['code']);
		if($result['result']){
			$result['message'] = lang('escort_hairstyleoption_delete_success');
		}else{
			$result['message'] = lang('escort_hairstyleoption_delete_error');
		}
		echo json_encode($result);
	}
}
?>